package com.usian.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.usian.yygh.cmn.client.DictCmnClient;
import com.usian.yygh.model.hosp.Hospital;
import com.usian.yygh.repository.HospitalRepository;
import com.usian.yygh.service.HospitalService;
import com.usian.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:WGG
 * @Date 2023年01月06日 17:52
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictCmnClient dictCmnClient;

    /**
     * 查询医院信息
     * @param hoscode
     */
    @Override
    public Hospital getHosptial(String hoscode) {
        Hospital getHospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return getHospital;
    }

    /**
     * 上传医院信息
     * @param map
     * @return
     */
    @Override
    public void save(Map<String, Object> map) {
        //将map集合转换成String
        String mapString = JSONObject.toJSONString(map);
        //将mapString转换成对象
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //根据code查找数据是否存在
        Hospital getHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        //判断
        if (getHospital!=null){
            //执行修改
            hospital.setStatus(hospital.getStatus());
            hospital.setCreateTime(hospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //执行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 医院列表查询
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //hospitalQueryVoz转换成hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建对象
        Example<Hospital> example = Example.of(hospital, matcher);
        //调用方法实现查询
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        //设置名称
        all.getContent().stream().forEach(item ->{
            this.getName(item);
        });
        return all;
    }

    /**
     * 根据医院名称获取医院列表
     * @param hosname
     * @return
     */
    @Override
    public Object findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue() == 0 || status.intValue() == 1) {
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 医院详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> show(String id) {
        Map<String, Object> result = new HashMap<>();

        Hospital hospital = getName(hospitalRepository.findById(id).get());
        result.put("hospital", hospital);

        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    /**
     * 获取医院名称
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        String hosname = hospital.getHosname();
        return hosname;
    }


    /**
     * 查询名称并设置
     * @param item
     */
    private Hospital getName(Hospital item) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictCmnClient.getName("Hostype", item.getHostype());
        //查询 省 市 地区
        String provienName = dictCmnClient.getName(item.getProvinceCode());
        String cityName = dictCmnClient.getName(item.getCityCode());
        String districtName = dictCmnClient.getName(item.getDistrictCode());

        item.getParam().put("hostypeString",hostypeString);
        item.getParam().put("fullAddress",provienName+cityName+districtName);
        return item;
    }
}
